import pymysql
from django.db import connection,transaction


def sqlexecute(strsql):
    # cursor= connection.cursor(cursor=pymysql.cursors.DictCursor)
    cursor=connection.cursor()
    cursor.execute(strsql)
    # ret=cursor.execute(strsql).iterator
    # print(ret)
    print('-----------------')
    result = [dict((cursor.description[i][0], value) for i, value in enumerate(row)) for row in
              cursor.fetchall()]

    print(result)

    transaction.commit()

    cursor.close()

    return result


# host='127.0.0.1'
# port=3306
# user='root'
# passwd='adminroot'
# db='bd2'
# conn=pymysql.connect(host=host,port=port,user=user,passwd=passwd,db=db)
# # 创建游标
# cursor=conn.cursor() # 默认返回数组
# cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
#     指定返回的是字典格式
# sql='CREATE TABLE test (id INT,name VARCHAR(20),INDEX index_name (name))'
# cursor.execute(sql)
# cursor.execute("INSERT INTO test(id,name)VALUES(1,'张三'),(2,'alex')")
# ret =cursor.execute("select * from test")
#
# print(ret)
# print(cursor.fetchone())
# cursor.scroll(-1,mode='relative')
# print(cursor.fetchone())
#
# cursor.scroll(1,mode='absolute')
# print(cursor.fetchone())
# print('-------------------')
# print(cursor.fetchall())
# conn.commit()
# cursor.close()
# conn.close()



